/*****************************************************************************
*
* File: spi.c
*
* Description: Contains functions to initialize SSEG display.
*
* Author: Kevin Casteel, Luis Castillo
*
* Revisions: v-1.0 (4/29/11)
*
******************************************************************************/

#include "spi.h"
#include <avr/io.h>


//--------------------------------------------------------------
//- Function: SPI_MasterInit()
//- 
//- return value: none
//- parameters: none
//-
//- Description: Initializes the AVR device as a master and 
//- and turns on the device and sets the clock frequency. 
//- For convenience, this function also configures the SPI
//- port GPIOs
//---------------------------------------------------------------
void SPI_MasterInit(void)
{
	/* Set MOSI and SCK output, all others input */
	DDRB = (1<<SPI_MOSI)|(1<<SPI_SCK)|(1<<SPI_SS);
	/* Enable SPI, Master, set clock rate fck/16 */
	SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
}



//--------------------------------------------------------------
//- Function: SPI_MasterTransmit()
//- 
//- return value: none
//- parameters: data to be transmitted
//-
//- Description: This function selects the device, transmits
//- the data, waits for a the transmission to complete with 
//- a poll, then deselects the device. 
//---------------------------------------------------------------
void SPI_MasterTransmit(uint8_t data)
{
	/* Start transmission */
	SPDR = data;
	/* Wait for transmission complete */
	while(!(SPSR & (1<<SPIF)))
		;
}
